package com.oceantrader.sale.commons.vertx;

import java.util.UUID;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import io.vertx.core.Vertx;
import io.vertx.ext.web.Router;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * Vertx Web Router配置
 * 
 * @author xuesinuo
 */
@Slf4j
@Configuration
@RequiredArgsConstructor
public class VertxRouterConfiguration {
    private final Vertx vertx;

    @Bean
    public Router router() {
        Router router = Router.router(vertx);
        router.errorHandler(500, http -> {
            if (http.response().ended()) {
                return;
            }
            String errorId = UUID.randomUUID().toString().substring(0, 8);
            Throwable t = http.failure();
            log.error("api error [" + errorId + "]", t);
            http.response().setStatusCode(500).send("System error (" + errorId + ").");
        });
        return router;
    }
}
